﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using main.sdk.display.object2d;

namespace main.src.game
{

    public class Game_Character : Sprite
    {

        public float? FallSpeed;
        public float? JumpSpeed;

        public float OldX;
        public bool IsMoving = false;

        public byte Direction = 4;

        public const int HORIZONTAL = 5;
        public const int VERTICAL   = 10;
        public const float GRAVITY    = 1.05f;

        public Rectangle Bounds;

        public void Jump()
        {
            FallSpeed = null;
            JumpSpeed = VERTICAL;
        }

        public void Fall()
        {
            JumpSpeed = null;
            FallSpeed = 1;
        }

        public void Move(byte direction)
        {
            UpdateMoving(direction);
        }

        public bool IsFalling() { return FallSpeed != null; }
        public bool IsJumping() { return JumpSpeed != null; }

        public virtual void Update()
        {
            Bounds.Width = (int)(Width * Scale.X);
            Bounds.Height = (int)(Height * Scale.Y);
            if (IsFalling())
                UpdateFalling();
            else if (IsJumping())
                UpdateJumping();
            XO = Bounds.X;
            YO = Bounds.Y;
            IsMoving = (Bounds.X != OldX);
            OldX = Bounds.X;
        }

        public void UpdateFalling()
        {
            FallSpeed *= GRAVITY;
            Bounds.Y += (int)FallSpeed.Value;
            foreach (Rectangle rectangle in Game_Map.Blocks)
                if (Bounds.Intersects(rectangle))
                {
                    Bounds.Y = rectangle.Y - Bounds.Height;
                    FallSpeed = null;
                    break;
                }
            foreach (Rectangle rectangle in Game_Map.Platforms)
                if (Bounds.Intersects(rectangle) && Bounds.Y <= rectangle.Y)
                {
                    Bounds.Y = rectangle.Y - Bounds.Height;
                    FallSpeed = null;
                    break;
                }
        }

        public void UpdateJumping()
        {
            JumpSpeed /= GRAVITY;
            Bounds.Y -= (int)JumpSpeed.Value;
            if ((int)JumpSpeed == 0)
                Fall();
            else
                foreach (Rectangle rectangle in Game_Map.Blocks)
                    if (Bounds.Intersects(rectangle))
                    {
                        Bounds.Y = rectangle.Bottom;
                        Fall();
                    }
        }

        public void UpdateMoving(byte direction)
        {
            Direction = direction;
            switch (Direction)
            {
                case 4:
                    Bounds.X += HORIZONTAL;
                    foreach (Rectangle rectangle in Game_Map.Blocks)
                        if (Bounds.Intersects(rectangle))
                        {
                            Bounds.X = rectangle.X - Bounds.Width;
                            break;
                        }
                    if (!(IsFalling() || IsJumping()))
                        Fall();
                    Effects = SpriteEffects.None;
                    break;
                case 6:
                    Bounds.X -= HORIZONTAL;
                    foreach (Rectangle rectangle in Game_Map.Blocks)
                        if (Bounds.Intersects(rectangle))
                        {
                            Bounds.X = rectangle.Right;
                            break;
                        }
                    if (!(IsFalling() || IsJumping()))
                        Fall();
                    Effects = SpriteEffects.FlipHorizontally;
                    break;
            }
        }

    }

}